﻿using RabbitMQ.Client;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Threading.Channels;
using System.Threading.Tasks;

namespace Demo.Net.RabbitMQ
{
    public class RabbitMQClient : IRabbitMQClient
    {

        private readonly RabbitMQClientOptions _rabbitMQClientOptions;
        private readonly ConnectionFactory _connectionFactory;
        public RabbitMQClient(RabbitMQClientOptions rabbitMQClientOption, ConnectionFactory connectionFactory)
        {
            _rabbitMQClientOptions = rabbitMQClientOption;
            _connectionFactory = connectionFactory;
        }



        /// <summary>
        /// Direct模式消息发送
        /// </summary>
        /// <param name="ququeName"></param>
        /// <param name="exchangeName"></param>
        /// <param name="message"></param>
        public void DirectPubilsh(string ququeName, string exchangeName, string message)
        {
            using (var connection = _connectionFactory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    channel.ExchangeDeclare(exchange: exchangeName, type: ExchangeType.Direct, durable: true, autoDelete: false, arguments: null);
                    channel.QueueDeclare(ququeName, true, false, false, null);
                    channel.QueueBind(ququeName, exchangeName, routingKey: string.Empty, arguments: null);
                    //发送消息
                    var body = Encoding.UTF8.GetBytes(message);
                    channel.BasicPublish(exchange: exchangeName, routingKey: string.Empty, basicProperties: null, body: body);
                }
            }
        }


        /// <summary>
        /// Fanout模式，发布订阅
        /// </summary>
        /// <param name="ququeNameList"></param>
        /// <param name="exchangeName"></param>
        /// <param name="message"></param>
        public void FanoutPublish(List<string> ququeNameList, string exchangeName, string message)
        {
            using (var connection = _connectionFactory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    channel.ExchangeDeclare(exchange: exchangeName, type: ExchangeType.Fanout, durable: true, autoDelete: false, arguments: null);

                    foreach (var ququeName in ququeNameList)
                    {
                        channel.QueueDeclare(ququeName, true, false, false, null);
                        channel.QueueBind(ququeName, exchangeName, routingKey: string.Empty, arguments: null);
                    }
                    //发送消息
                    var body = Encoding.UTF8.GetBytes(message);
                    channel.BasicPublish(exchange: exchangeName, routingKey: string.Empty, basicProperties: null, body: body);
                }
            }
        }


        /// <summary>
        /// Header模式
        /// </summary>
        /// <param name="ququeName"></param>
        /// <param name="exchangeName"></param>
        /// <param name="message"></param>
        public void HeaderPublish(string exchangeName, string message, Dictionary<string, object> headers, string ququeName = null)
        {
            using (var connection = _connectionFactory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {

                    channel.ExchangeDeclare(exchange: exchangeName, type: ExchangeType.Headers, durable: true, autoDelete: false, arguments: null);

                    if (!string.IsNullOrEmpty(ququeName))
                    {
                        channel.QueueDeclare(queue: ququeName, durable: false, exclusive: false, autoDelete: false, arguments: null);
                        channel.QueueBind(queue: ququeName, exchange: exchangeName, routingKey: string.Empty, arguments: null);
                    }
                    // 发布消息到Exchange，通过设置BasicProperties中的Headers属性指定Header信息
                    var properties = channel.CreateBasicProperties();
                    properties.Persistent = true;
                    properties.Headers = headers;
                    var body = Encoding.UTF8.GetBytes(message);
                    channel.BasicPublish(exchangeName,string.Empty, basicProperties: properties, body: body);

                }
            }
        }


        /// <summary>
        /// Topic模式
        /// </summary>
        public void TopicPublish(string exchangeName,string message,string routingKey,string ququeName = null)
        {
            using (var connection = _connectionFactory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {

                    channel.ExchangeDeclare(exchange: exchangeName, type: ExchangeType.Topic, durable: true, autoDelete: false, arguments: null);

                    if (!string.IsNullOrEmpty(ququeName))
                    {
                        channel.QueueDeclare(queue: ququeName, durable: false, exclusive: false, autoDelete: false, arguments: null);
                        channel.QueueBind(queue: ququeName, exchange: exchangeName, routingKey:routingKey, arguments: null);
                    }
                    // 发布消息到Exchange，通过设置BasicProperties中的routingKey
                 
                    var body = Encoding.UTF8.GetBytes(message);
                    channel.BasicPublish(exchangeName, routingKey:routingKey, basicProperties:null, body: body);

                }
            }
        }
    }
}
